home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue59 / IBSec / WinCrypt.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2000-03-30  |  16.2 KB  |  461 lines

  1. Unit WinCrypt;
  2.  
  3. {
  4. WINCRYPT.PAS conversion from C header file WINCRPYT.C
  5. Jani JΣrvinen 1998.
  6. }
  7.  
  8. Interface
  9.  
  10. Uses Windows;
  11.  
  12. Function GetAlgClass(AClass : Integer) : Integer;
  13. Function GetAlgType(AType : Integer) : Integer;
  14. Function GetAlgSID(SID : Integer) : Integer;
  15.  
  16. Function RCryptSucceeded(rt : Boolean) : Boolean;
  17. Function RCryptFailed(rt : Boolean) : Boolean;
  18.  
  19. { Algorithm classes }
  20. Const
  21.   AlgClassAny         = 0;
  22.   AlgClassSignature   = 1 shl 13;
  23.   AlgClassMsgEncrypt  = 2 shl 13;
  24.   AlgClassDataEncrypt = 3 shl 13;
  25.   AlgClassHash        = 4 shl 13;
  26.   AlgClassKeyExchange = 5 shl 13;
  27.  
  28. { Algorithm types }
  29.   AlgTypeAny    = 0;
  30.   AlgTypeDSS    = 1 shl 9;
  31.   AlgTypeRSA    = 2 shl 9;
  32.   AlgTypeBlock  = 3 shl 9;
  33.   AlgTypeStream = 4 shl 9;
  34.  
  35. { Generic sub-ids }
  36.   AlgSIDAny = 0;
  37.  
  38. { Some RSA sub-ids }
  39.   AlgSIDRSAAny      = 0;
  40.   AlgSIDRSAPKCS     = 1;
  41.   AlgSIDRSAMSAtWork = 2;
  42.   AlgSIDRSAEntrust  = 3;
  43.   AlgSIDRSAPGP      = 4;
  44.  
  45. { Some DSS sub-ids }
  46.   AlgSIDDSSAny  = 0;
  47.   AlgSIDDSSPKCS = 1;
  48.   AlgSIDDSSDMS  = 2;
  49.  
  50. { Block cipher sub ids }
  51.   AlgSIDDES        = 1; { DES subids }
  52.   AlgSID3DES       = 3;
  53.   AlgSIDDESX       = 4;
  54.   AlgSIDIDEA       = 5;
  55.   AlgSIDCAST       = 6;
  56.   AlgSIDSaferSK64  = 7;
  57.   ALDSIDSaferSK128 = 8;
  58.   { KP_MODE }
  59.   CryptModeCBCI    = 6;    { ANSI CBC Interleaved       }
  60.   CryptModeCFBP    = 7;    { ANSI CFB Pipelined         }
  61.   CryptModeOFBP    = 8;    { ANSI OFB Pipelined         }
  62.   CryptModeCBCOFM  = 9;    { ANSI CBC + OF Masking      }
  63.   CryptModeCBCOFMI = 10;   { ANSI CBC + OFM Interleaved }
  64.  
  65. { RC2 sub-ids }
  66.   AlgSIDRC2 = 2;
  67.  
  68. { Stream cipher sub-ids }
  69.   AlgSIDRC4  = 1;
  70.   AlgSIDSEAL = 2;
  71.  
  72. { Hash sub ids }
  73.   AlgSIDMD2        = 1;
  74.   AlgSIDMD4        = 2;
  75.   AlgSIDMD5        = 3;
  76.   AlgSIDSHA        = 4;
  77.   AlgSIDMAC        = 5;
  78.   AlgSIDRIPEMD     = 6;
  79.   AlgSIDRIPEMD160  = 7;
  80.   AlgSIDSSL3SHAMD5 = 8;
  81.  
  82. { Our silly example sub-id }
  83.   AlgSIDExample = 80;
  84.  
  85. { (Class) algorithm identifier definitions }
  86.   CAlgMD2     = (AlgClassHash Or AlgTypeAny Or AlgSIDMD2);
  87.   CAlgMD4     = (AlgClassHash Or AlgTypeAny Or AlgSIDMD4);
  88.   CAlgMD5     = (AlgClassHash Or AlgTypeAny Or AlgSIDMD5);
  89.   CAlgSHA     = (AlgClassHash Or AlgTypeAny Or AlgSIDSHA);
  90.   CAlgMAC     = (AlgClassHash Or AlgTypeAny Or AlgSIDMAC);
  91.   CAlgRSASign = (AlgClassSignature Or AlgTypeRSA Or AlgSIDRSAAny);
  92.   CAlgDSSSign = (AlgClassSignature Or AlgTypeDSS Or AlgSIDDSSAny);
  93.   CAlgRSAKeyX = (AlgClassKeyExchange Or AlgTypeRSA Or AlgSIDRSAAny);
  94.   CAlgDES     = (AlgClassDataEncrypt Or AlgTypeBlock Or AlgSIDDES);
  95.   CAlgRC2     = (AlgClassDataEncrypt Or AlgTypeBlock Or AlgSIDRC2);
  96.   CAlgRC4     = (AlgClassDataEncrypt Or AlgTypeStream Or AlgSIDRC4);
  97.   CAlgSeal    = (AlgClassDataEncrypt Or AlgTypeStream Or AlgSIDSeal);
  98.  
  99. Type
  100.   PVTableProvStruc = ^TVTableProvStruc;
  101.   TVTableProvStruc = Record
  102.     Version         : Integer;
  103.     FuncVerifyImage : TFarProc;
  104.     FuncReturnhWnd  : TFarProc;
  105.   End;
  106.  
  107.   TCryptProv = Integer;
  108.   TCryptKey  = Integer;
  109.   TCryptHash = Integer;
  110.   TAlgId     = Integer;
  111.   PCryptProv = ^TCryptProv;
  112.   PCryptKey  = ^TCryptKey;
  113.   PCryptHash = ^TCryptHash;
  114.   PAlgId     = ^TAlgId;
  115.  
  116. Const
  117. { dwFlags definitions for CryptAquireContext }
  118.   CryptVerfiyContext  = $F0000000;
  119.   CryptNewKeySet      = 8;
  120.   CryptDeleteKeySet   = 16;
  121.  
  122. { dwFlag definitions for CryptGenKey }
  123.   CryptExportable     = 1;
  124.   CryptUserProtected  = 2;
  125.   CryptCreateSalt     = 4;
  126.   CryptUpdateKey      = 8;
  127.  
  128. { exported key blob definitions }
  129.   SimpleBLOB          = 1;
  130.   PublicKeyBLOB       = 6;
  131.   PricateKeyBLOB      = 7;
  132.  
  133.   ATKeyExchange       = 1;
  134.   ATSignature         = 2;
  135.  
  136.   CryptUserData       = 1;
  137.  
  138. { dwParam }
  139.   KPIV                = 1;       { Initialization vector      }
  140.   KPSalt              = 2;       { Salt value                 }
  141.   KPPadding           = 3;       { Padding values             }
  142.   KPMode              = 4;       { Mode of the cipher         }
  143.   KPModeBits          = 5;       { Number of bits to feedback }
  144.   KPPermissions       = 6;       { Key permissions DWORD      }
  145.   KPAlgID             = 7;       { Key algorithm              }
  146.   KPBlockLen          = 8;       { Block size of the cipher   }
  147.  
  148. { KP_PADDING }
  149.   PKCS5Padding        = 1;       { PKCS 5 (sec 6.2) padding method }
  150.  
  151. { KP_MODE }
  152.   CryptModeCBC        = 1;       { Cipher block chaining    }
  153.   CryptModeECB        = 2;       { Electronic code book     }
  154.   CryptModeOFB        = 3;       { Output feedback mode     }
  155.   CryptModeCFB        = 4;       { Cipher feedback mode     }
  156.   CryptModeCTS        = 5;       { Ciphertext stealing mode }
  157.  
  158. { KP_PERMISSIONS }
  159.   Crypt_Encrypt       = 1;       { Allow encryption               }
  160.   Crypt_Decrypt       = 2;       { Allow decryption               }
  161.   CryptExport         = 4;       { Allow key to be exported       }
  162.   CryptRead           = 8;       { Allow parameters to be read    }
  163.   CryptWWrite         = 16;      { Allow parameters to be set     }
  164.   CryptMAC            = 32;      { Allow MACs to be used with key }
  165.  
  166.   HPAlgID             = 1;       { Hash algorithm  }
  167.   HPHashVal           = 2;       { Hash value      }
  168.   HPHashSize          = 4;       { Hash value size }
  169.  
  170.   CryptFailed         = False;
  171.   CryptSucceed        = True;
  172.  
  173. Const
  174. { CryptGetProvParam }
  175.   PPEnumAlgs          = 1;
  176.   PPEnumContainers    = 2;
  177.   PPImpType           = 3;
  178.   PPName              = 4;
  179.   PPVersion           = 5;
  180.   PPContainer         = 6;
  181.  
  182.   CryptFirst          = 1;
  183.   CryptNext           = 2;
  184.  
  185.   CryptImplHardware   = 1;
  186.   CryptImplSoftware   = 2;
  187.   CryptImplMixed      = 3;
  188.   CryptImplUnknown    = 4;
  189.  
  190. { CryptSetProvParam }
  191.   PPClienthWnd        = 1;
  192.  
  193.   ProvRSAFull         = 1;
  194.   ProvRSASIG          = 2;
  195.   ProvDSS             = 3;
  196.   ProvFortezza        = 4;
  197.   ProvMSExchange      = 5;
  198.   ProvSSL             = 6;
  199.  
  200. { STT defined Providers }
  201.   ProvSTTMER          = 7;
  202.   ProvSTTACQ          = 8;
  203.   ProvSTTBRND         = 9;
  204.   ProvSTTRoot         = 10;
  205.   ProvSTTISS          = 11;
  206.  
  207.   ProvText            = 'Microsoft Base Cryptographic Provider v1.0';
  208.   MSDefProvA          : PAnsiChar = ProvText;
  209.   MSDefProvW          : PWideChar = ProvText;
  210.   MSDefProv           : PAnsiChar = ProvText;
  211.  
  212.   MaxUIDLen           = 64;
  213.   CurBLOBVersion      = 2;
  214.  
  215. Type
  216.   TProvEnumAlgs = Record
  217.     aiAlgid   : TAlgID;
  218.     dwBitLen  : Integer;
  219.     dwNameLen : Integer;
  220.     szName    : Array[0..19] of Char;
  221.   End;
  222.  
  223.   TPublicKeyStruc = Record
  224.     bType    : Byte;
  225.     bVersion : Byte;
  226.     Reserved : Word;
  227.     aiKeyAlg : TAlgID;
  228.   End;
  229.  
  230.   TRSAPubKey = Record
  231.     Magic  : Integer;                 { Has to be RSA1        }
  232.     BitLen : Integer;                 { # of bits in modulus  }
  233.     PubExp : Integer;                 { public exponent       }
  234.   End;                                { modulus data follows  }
  235.  
  236. Function CryptAcquireContextA(phProv : PCryptProv;
  237.                               pszContainer,pszProvider : PChar;
  238.                               dwProvType,dwFlags : Integer) : Bool; StdCall;
  239.  
  240. Function CryptAcquireContextW(phProv : PCryptProv;
  241.                               pszContainer,pszProvider : PWideChar;
  242.                               dwProvType,dwFlags : Integer) : Bool; StdCall;
  243.  
  244. Function CryptAcquireContext(phProv : PCryptProv;
  245.                              pszContainer,pszProvider : PChar;
  246.                              dwProvType,dwFlags : Integer) : Bool; StdCall;
  247.  
  248. Function CryptReleaseContext(hProv : TCryptProv; dwFlags : Integer) : Bool; StdCall;
  249.  
  250. Function CryptGenKey(hProv : TCryptProv; Algid : TAlgID; dwFlags : Integer;
  251.                      phKey : PCryptKey) : Bool; StdCall;
  252.  
  253. Function CryptDeriveKey(hProv : TCryptProv;
  254.                         Algid : TAlgID;
  255.                         hBaseData : TCryptHash;
  256.                         dwFlags : Integer;
  257.                         phKey : PCryptKey) : Bool; StdCall;
  258.  
  259.  
  260. Function CryptDestroyKey(hKey : TCryptKey) : Bool; StdCall;
  261.  
  262. Function CryptSetKeyParam(hKey : TCryptKey;
  263.                           dwParam : Integer;
  264.                           pbData : Pointer;
  265.                           dwFlags : Integer) : Bool; StdCall;
  266.  
  267. Function CryptGetKeyParam(hKey : TCryptKey;
  268.                           dwParam : Integer;
  269.                           pbData : Pointer;
  270.                           Var pdwDataLen : Integer;
  271.                           dwFlags : Integer) : Bool; StdCall;
  272.  
  273. Function CryptSetHashParam(hHash : TCryptHash;
  274.                            dwParam : Integer;
  275.                            pbData : Pointer;
  276.                            dwFlags : Integer) : Bool; StdCall;
  277.  
  278. Function CryptGetHashParam(hHash : TCryptHash;
  279.                            dwParam : Integer;
  280.                            pbData : Pointer;
  281.                            Var pdwDataLen : Integer;
  282.                            dwFlags : Integer) : Bool; StdCall;
  283.  
  284. Function CryptSetProvParam(hProv : TCryptProv;
  285.                            dwParam : Integer;
  286.                            pbData : Pointer;
  287.                            dwFlags : Integer) : Bool; StdCall;
  288.  
  289. Function CryptGetProvParam(hProv : TCryptProv;
  290.                            dwParam : Integer;
  291.                            pbData : Pointer;
  292.                            Var pdwDataLen : Integer;
  293.                            dwFlags : Integer) : Bool; StdCall;
  294.  
  295. Function CryptGenRandom(hProv : TCryptProv;
  296.                         dwLen : Integer;
  297.                         pbBuffer : Pointer) : Bool; StdCall;
  298.  
  299. Function CryptGetUserKey(hProv : TCryptProv;
  300.                          dwKeySpec : Integer;
  301.                          phUserKey : PCryptKey) : Bool; StdCall;
  302.  
  303. Function CryptExportKey(hKey,hExpKey : TCryptKey;
  304.                         dwBlobType,dwFlags : Integer;
  305.                         pbData : Pointer;
  306.                         Var pdwDataLen : Integer) : Bool; StdCall;
  307.  
  308. Function CryptImportKey(hProv : TCryptProv;
  309.                         Const pbData : Pointer;
  310.                         dwDataLen : Integer;
  311.                         hPubKey : TCryptKey;
  312.                         dwFlags : Integer;
  313.                         phKey : PCryptKey) : Bool; StdCall;
  314.  
  315. Function CryptEncrypt(hKey : TCryptKey;
  316.                       hHash : TCryptHash;
  317.                       Final : Bool;
  318.                       dwFlags : Integer;
  319.                       pbData : Pointer;
  320.                       Var pdwDataLen : Integer;
  321.                       dwBufLen : Integer) : Bool; StdCall;
  322.  
  323. Function CryptDecrypt(hKey : TCryptKey;
  324.                       hHash : TCryptHash;
  325.                       Final : Bool;
  326.                       dwFlags : Integer;
  327.                       pbData : Pointer;
  328.                       Var pdwDataLen : Integer) : Bool; StdCall;
  329.  
  330. Function CryptCreateHash(hProv : TCryptProv;
  331.                          Algid : TAlgID;
  332.                          hKey : TCryptKey;
  333.                          dwFlags : Integer;
  334.                          phHash : PCryptHash) : Bool; StdCall;
  335.  
  336. Function CryptHashData(hHash : TCryptHash;
  337.                        Const pbData : Pointer;
  338.                        dwDataLen,dwFlags : Integer) : Bool; StdCall;
  339.  
  340. Function CryptHashSessionKey(hHash : TCryptHash;
  341.                              hKey : TCryptKey;
  342.                              dwFlags : Integer) : Bool; StdCall;
  343.  
  344. Function CryptDestroyHash(hHash : TCryptHash) : Bool; StdCall;
  345.  
  346. Function CryptSignHashA(hHash : TCryptHash;
  347.                         dwKeySpec : Integer;
  348.                         sDescription : PChar;
  349.                         dwFlags : Integer;
  350.                         pbSignature : Pointer;
  351.                         Var pdwSigLen : Integer) : Bool; StdCall;
  352.  
  353. Function CryptSignHashW(hHash : TCryptHash;
  354.                         dwKeySpec : Integer;
  355.                         sDescription : PWideChar;
  356.                         dwFlags : Integer;
  357.                         pbSignature : Pointer;
  358.                         pdwSigLen : Integer) : Bool; StdCall;
  359.  
  360. Function CryptSignHash(hHash : TCryptHash;
  361.                        dwKeySpec : Integer;
  362.                        sDescription : PChar;
  363.                        dwFlags : Integer;
  364.                        pbSignature : Pointer;
  365.                        Var pdwSigLen : Integer) : Bool; StdCall;
  366.  
  367. Function CryptVerifySignatureA(hHash : TCryptHash;
  368.                                Const pbSignature : Pointer;
  369.                                dwSigLen : Integer;
  370.                                hPubKey : TCryptKey;
  371.                                sDescription : PChar;
  372.                                dwFlags : Integer) : Bool; StdCall;
  373.  
  374. Function CryptVerifySignatureW(hHash : TCryptHash;
  375.                                Const pbSignature : Pointer;
  376.                                dwSigLen : Integer;
  377.                                hPubKey : TCryptKey;
  378.                                sDescription : PWideChar;
  379.                                dwFlags : Integer) : Bool; StdCall;
  380.  
  381. Function CryptVerifySignature(hHash : TCryptHash;
  382.                               Const pbSignature : Pointer;
  383.                               dwSigLen : Integer;
  384.                               hPubKey : TCryptKey;
  385.                               sDescription : PChar;
  386.                               dwFlags : Integer) : Bool; StdCall;
  387.  
  388. Function CryptSetProviderA(pszProvName : PChar;
  389.                            dwProvType : Integer) : Bool; StdCall;
  390.  
  391. Function CryptSetProviderW(pszProvName : PWideChar;
  392.                            dwProvType : Integer) : Bool; StdCall;
  393.  
  394. Function CryptSetProvider(pszProvName : PChar;
  395.                           dwProvType : Integer) : Bool; StdCall;
  396.  
  397. Implementation
  398.  
  399. { Algorithm IDs and Flags }
  400.  
  401. { Alg_ID crackers }
  402. Function GetAlgClass(AClass : Integer) : Integer;
  403. Begin
  404.   Result := (AClass And (7 shl 13));
  405. End;
  406.  
  407. Function GetAlgType(AType : Integer) : Integer;
  408. Begin
  409.   Result :=  (AType And (15 shl 9));
  410. End;
  411.  
  412. Function GetAlgSID(SID : Integer) : Integer;
  413. Begin
  414.   Result := (SID And 511);
  415. End;
  416.  
  417. Function RCryptSucceeded(rt : Boolean) : Boolean;
  418. Begin
  419.   Result :=  (rt = CryptSucceed);
  420. End;
  421.  
  422. Function RCryptFailed(rt : Boolean) : Boolean;
  423. Begin
  424.   Result := (rt = CryptFailed);
  425. End;
  426.  
  427. Function CryptAcquireContextA; External advapi32 Name 'CryptAcquireContextA';
  428. Function CryptAcquireContextW; External advapi32 Name 'CryptAcquireContextW';
  429. Function CryptAcquireContext; External advapi32 Name 'CryptAcquireContextA';
  430. Function CryptReleaseContext; External advapi32 Name 'CryptReleaseContext';
  431. Function CryptGenKey; External advapi32 Name 'CryptGenKey';
  432. Function CryptDeriveKey; External advapi32 Name 'CryptDeriveKey';
  433. Function CryptDestroyKey; External advapi32 Name 'CryptDestroyKey';
  434. Function CryptSetKeyParam; External advapi32 Name 'CryptSetKeyParam';
  435. Function CryptGetKeyParam; External advapi32 Name 'CryptGetKeyParam';
  436. Function CryptSetHashParam; External advapi32 Name 'CryptSetHashParam';
  437. Function CryptGetHashParam; External advapi32 Name 'CryptGetHashParam';
  438. Function CryptSetProvParam; External advapi32 Name 'CryptSetProvParam';
  439. Function CryptGetProvParam; External advapi32 Name 'CryptGetProvParam';
  440. Function CryptGenRandom; External advapi32 Name 'CryptGenRandom';
  441. Function CryptGetUserKey; External advapi32 Name 'CryptGetUserKey';
  442. Function CryptExportKey; External advapi32 Name 'CryptExportKey';
  443. Function CryptImportKey; External advapi32 Name 'CryptImportKey';
  444. Function CryptEncrypt; External advapi32 Name 'CryptEncrypt';
  445. Function CryptDecrypt; External advapi32 Name 'CryptDecrypt';
  446. Function CryptCreateHash; External advapi32 Name 'CryptCreateHash';
  447. Function CryptHashData; External advapi32 Name 'CryptHashData';
  448. Function CryptHashSessionKey; External advapi32 Name 'CryptHashSessionKey';
  449. Function CryptDestroyHash; External advapi32 Name 'CryptDestroyHash';
  450. Function CryptSignHashA; External advapi32 Name 'CryptSignHashA';
  451. Function CryptSignHashW; External advapi32 Name 'CryptSignHashW';
  452. Function CryptSignHash; External advapi32 Name 'CryptSignHashA';
  453. Function CryptVerifySignatureA; External advapi32 Name 'CryptVerifySignatureA';
  454. Function CryptVerifySignatureW; External advapi32 Name 'CryptVerifySignatureW';
  455. Function CryptVerifySignature; External advapi32 Name 'CryptVerifySignatureA';
  456. Function CryptSetProviderA; External advapi32 Name 'CryptSetProviderA';
  457. Function CryptSetProviderW; External advapi32 Name 'CryptSetProviderW';
  458. Function CryptSetProvider; External advapi32 Name 'CryptSetProviderA';
  459.  
  460. End.
  461.